/**
 * Created by MadFrog on 2017/10/13.
 * 课堂管理
 * 主要功能：课堂的增删改查，
 */

Ext.namespace("admin.student");

admin.student.lesson = function(panelId, title, iconClz){
    var courseCombo = ExtComponentUtil.generateCourseComboByTeacher();
    var queryBar = ["->", "课程:", courseCombo,{
        text : "查询",
        iconCls : "query",
        handler : function(){
            lessonStore.reload();
        }
    }];
    var optBar = genOptBarByFn(createLesson, deleteLesson, updateLesson, toggleStatusLesson);
    optBar.push({
        text : "二维码",
        iconCls : "barcode",
        handler : generateBarcode
    });

    var lessonStore = new Ext.data.JsonStore({
        url : "admin/lesson/listByCourse",
        root : "data",
        fields : ["id", "startTime", "clazzNames", "status"],
        listeners : {
            beforeload : function(store){
                store.removeAll();
                Ext.apply(store.baseParams, {
                    courseId : courseCombo.getValue()
                });
            }
        }
    });
    var lessonCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header : "上课班级",
        dataIndex : "clazzNames",
        width : 150
    }, {
        header : "上课时间",
        dataIndex : "startTime",
        renderer : renderTime2Minutes,
        width : 130
    }, {
        header : "教室",
        dataIndex : "clazzRoom"
    }, {
        header : "状态",
        dataIndex : "status",
        renderer : renderStatus
    }]);
    var lessonSm = new Ext.grid.RowSelectionModel({
        singleSelect : true,
        listeners : {
            rowselect : function(){
                var rec = lessonSm.getSelected();
                studentStore.load();
            }
        }
    });
    var lessonGrid = new Ext.grid.GridPanel({
        title : "课堂",
        flex : 3,
        frame : true,
        sm : lessonSm,
        cm : lessonCm,
        store : lessonStore,
        tbar : genToolBar(optBar, queryBar, panelId),
    });

    var studentStore = new Ext.data.JsonStore({
        url : "admin/studentLesson/listStudentByCon",
        root : "data",
        fields : ["id", "name", "xh", "bjmc", "signInStatus"],
        listeners : {
            beforeload : function(s){
                s.removeAll();
                Ext.apply(s.baseParams, {
                    lessonId : lessonSm.getSelected().get("id"),
                    signIn : signInCombo.getValue()
                });
            }
        }
    });
    var studentCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header : "班级",
        dataIndex : "bjmc"
    }, {
        header : "姓名",
        dataIndex : "name"
    }, {
        header : "学号",
        dataIndex : "xh"
    }, {
        header : "签到",
        dataIndex : "signInStatus",
        // renderer : renderSignInStatus
        renderer : ExtRenderUtil.renderSignIn
    }]);
    var studentSm = new Ext.grid.RowSelectionModel({
        singleSelect : true
    });
    var signInCombo = new Ext.form.ComboBox({
        fieldLabel : "签到情况",
        store : [["0", "未签到"], ["1", "已签到"]],
        editable : false,
        width : 70,
        mode : "local",
        triggerAction : "all",
        valueField : "signIn",
        displayField : "display",
        hiddenName : "signIn",
        resizable : true
    });
    var studentQueryBar = ["->", "签到状态:", signInCombo, {
        xtype : "button",
        text : "查询",
        iconCls : "query",
        handler : function () {
            var rec = lessonSm.getSelected();
            if(rec==null){
                showFailMsg("请选择课堂");
                return;
            }
            studentStore.reload();
        }
    }];
    var studentGrid = new Ext.grid.GridPanel({
        title : "学生签到情况",
        flex : 3,
        frame : true,
        sm : studentSm,
        cm : studentCm,
        store : studentStore,
        tbar : studentQueryBar
    });
    return new Ext.Panel({
        id : panelId,
        title : title,
        iconCls : iconClz,
        closable : true,
        layout : "hbox",
        layoutConfig : {
            align : "stretch"
        },
        items : [lessonGrid, studentGrid]
    });

    function createLesson() {
        var win = genLessonAddOrUpdateWin("admin/lesson/add");
        win.show();
    }
    function deleteLesson() {
        var rec = lessonSm.getSelected();
        if(rec==null){
            showFailMsg("请选择课堂");
            return;
        }
        confirmDelete(function(){
            ajaxRequest({
                url : "admin/lesson/delete/" + rec.get("id"),
                callback : function(){
                    lessonStore.load();
                }
            });
        });
    }
    function updateLesson() {
        var rec = lessonSm.getSelected();
        if(rec==null){
            showFailMsg("请选择课堂");
            return;
        }
        var win = genLessonAddOrUpdateWin("admin/lesson/update");
        win.findByType("form")[0].getForm().loadRecord(rec);
        win.show();
    }
    function  toggleStatusLesson() {
        var rec = lessonSm.getSelected();
        if(rec==null){
            showFailMsg("请选择课堂");
            return;
        }
        ajaxRequest({
            url : "admin/lesson/toggleStatus/" +rec.get("id"),
            callback : function(){
                studentStore.load();
            }
        });
    }

    function genLessonAddOrUpdateWin(url){
        return new Ext.Window({
            title : "新增或更新课堂",
            layout : "fit",
            modal : true,
            iconCls : "save",
            width : 300,
            height : 230,
            plain : true,
            bodyStyle : 'padding:5px;',
            items : genForm(url)
        });
        function genForm(url){
            var courseCombo = ExtComponentUtil.generateCourseComboByTeacher();
            var clazzCombo = ExtComponentUtil.generateClassMultiComboBox();
            var clazzNamesHidden = new Ext.form.Hidden({
                name : "clazzNames",
                value : ""
            });
            return new Ext.form.FormPanel({
                url : url,
                labelWidth : 70,
                labelAlign : "right",
                baseCls: 'x-plain',
                defaults : {
                    xtype : "textfield",
                    anchor : "95%"
                },
                items : [{
                    xtype : "hidden",
                    name : "id",
                    value : "-1"
                }, courseCombo, clazzCombo, {
                    fieldLabel : "开始时间",
                    allowBlank : false,
                    name : "startTime",
                    xtype : "datetimefield"
                }, {
                    fieldLabel:"上课地点",
                    name : "clazzRoom"
                }, genEnumStatusCombo(), clazzNamesHidden],
                buttonAlign : "center",
                buttons : [{
                    text : "保存",
                    iconCls : "save",
                    handler : function(b){
                        clazzNamesHidden.setValue(clazzCombo.getCheckedDisplay());
                        submitForm(b.findParentByType("form"), refreshGrid);
                        function refreshGrid(){

                        }
                    }
                }, {
                    text : "重置",
                    iconCls : "reset",
                    type : "reset",
                    handler : function(b){
                        b.findParentByType("form").getForm().reset();
                    }
                }]
            });
        }
    }
    function generateBarcode() {
        var rec = lessonSm.getSelected();
        if(rec==null){
            showFailMsg("请选择课堂");
            return;
        }
        var lessonId = rec.get("id");
        window.open("admin/lesson/generateBarcode/" + lessonId);
    }

    // function genLessonAddOrUpdateWin(url){
    //     return new Ext.Window({
    //         title : "新增或更新课堂",
    //         layout : "fit",
    //         modal : true,
    //         iconCls : "save",
    //         width : 300,
    //         height : 200,
    //         plain : true,
    //         bodyStyle : 'padding:5px;',
    //         items : genForm(url)
    //     });
    //     function genForm(url){
    //         return new Ext.form.FormPanel({
    //             url : url,
    //             labelWidth : 70,
    //             labelAlign : "right",
    //             baseCls: 'x-plain',
    //             defaults : {
    //                 xtype : "textfield",
    //                 anchor : "95%"
    //             },
    //             items : [{
    //                 xtype : "hidden",
    //                 name : "id",
    //                 value : "-1"
    //             }, {
    //                 fieldLabel:"班级名称",
    //                 name : "name"
    //             }, {
    //                 fieldLabel : "活动时间",
    //                 allowBlank : false,
    //                 name : "startTime",
    //                 xtype : "datetimefield"
    //             }, {
    //                 fieldLabel:"上课地点",
    //                 name : "clazzRoom"
    //             }],
    //             buttonAlign : "center",
    //             buttons : [{
    //                 text : "保存",
    //                 iconCls : "save",
    //                 handler : function(b){
    //                     submitForm(b.findParentByType("form"), refreshGrid);
    //                     function refreshGrid(){
    //                         clazzStore.load();
    //                     }
    //                 }
    //             }, {
    //                 text : "重置",
    //                 iconCls : "reset",
    //                 type : "reset",
    //                 handler : function(b){
    //                     b.findParentByType("form").getForm().reset();
    //                 }
    //             }]
    //         });
    //     }
    // }
}

registerModule("admin.student.lesson", admin.student.lesson);